package com.bjpowernode.service.impl;

import cn.hutool.core.util.IdUtil;
import com.bjpowernode.constant.Constant;
import com.bjpowernode.entity.TTran;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.query.TranQuery;
import com.bjpowernode.service.TranService;
import com.bjpowernode.util.LoginInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class TranServiceImpl implements TranService {

    private final TTranMapper tTranMapper;

    @Override
    public int createTran(TranQuery tranQuery) {
        TTran tTran = new TTran();
        BeanUtils.copyProperties(tranQuery, tTran);
        //交易流水号 1.uuid 没有规律   2. 雪花算法 有规律  时间戳 机房编号 机器编号 自增值
        //String uuid = UUID.randomUUID().toString();//没有规律
        long snowflakeNextId = IdUtil.getSnowflakeNextId();
        tTran.setTranNo(String.valueOf(snowflakeNextId));

        tTran.setCreateTime(new Date());
        tTran.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());

        return tTranMapper.insertSelective(tTran);
    }

    @Override
    public PageInfo<TTran> getTranByPage(Integer current) {
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        List<TTran> tTranList = tTranMapper.selectByPage();
        PageInfo<TTran> pageInfo = new PageInfo<>(tTranList);
        return pageInfo;
    }

    @Override
    public TTran getTranById(Integer id) {
        return tTranMapper.selectById( id);
    }
}
